package com.yixun.qfbao.controller;

import com.alibaba.fastjson.JSON;
import com.yixun.qfbao.api.service.DeptPermissionService;
import com.yixun.qfbao.api.service.WHReportService;
import com.yixun.qfbao.hander.*;
import com.yixun.qfbao.model.whdto.SyncReportDto;
import com.yixun.qfbao.request.GenericIdRequest;
import com.yixun.qfbao.utils.JSONUtil;
import com.yixun.qfbao.vo.ImportCooOrgReqVo;
import com.yixun.qfbao.vo.InitDataReqVo;
import com.yixun.qfbao.vo.InitReportCommissionDataReqVo;
import com.yixun.qfbao.vo.SendWXMsgAgainReqVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/init/tools")
public class InitDataController {
	
	private static final Logger logger = LoggerFactory.getLogger(InitDataController.class);

	@Autowired
	SendWXMsgAgainHander sendWXMsgAgainHander;
	@Autowired
	ImportPremisesHander importPremisesHander;
	@Autowired
	ImportPremisesHouseTypeHander importPremisesHouseTypeHander;
	@Autowired
	ImportPremisesDynamicHander importPremisesDynamicHander;
	@Autowired
	ImportPremisesImageHander importPremisesImageHander;
	@Autowired
	ImportPremisesCommissionHander importPremisesCommissionHander;
	@Autowired
	InitReportCommissionDataHander initReportCommissionDataHander;
	@Autowired
	WHReportService whReportService;
	@Autowired
	DeptPermissionService deptPermissionService;

	/**
	 * 初始化报备佣金渠道商数据
	 *
	 * @param request
	 * @param reqVo
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/initReportCommission", produces = "application/json")
	public String initReportCommission(HttpServletRequest request, @RequestBody InitReportCommissionDataReqVo reqVo)
			throws IOException {
		try {
			return JSONUtil.toJSONStringWithDateFormat(initReportCommissionDataHander.hander(reqVo));
		} catch (Exception e) {
			logger.error("initReportCommission error: {}", e);
			return null;
		}
	}

	/**
	 * 批量导入V10楼盘
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping(value = "/importV10Premises", produces = "application/json")
	public String importV10Premises(MultipartHttpServletRequest request, ImportCooOrgReqVo importCooOrgReqVo) {
		try {
			importCooOrgReqVo.setMulRequest(request);
			return JSONUtil.toJSONStringWithDateFormat(importPremisesHander.doBusiness(importCooOrgReqVo));
		} catch (Exception e) {
			logger.error("importV10Premises error: {}", e);
			return null;
		}
	}

	/**
	 * 批量导入V10楼盘户型
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping(value = "/importV10PremisesHouseType", produces = "application/json")
	public String importV10PremisesHouseType(MultipartHttpServletRequest request, ImportCooOrgReqVo importCooOrgReqVo) {
		try {
			importCooOrgReqVo.setMulRequest(request);
			return JSONUtil.toJSONStringWithDateFormat(importPremisesHouseTypeHander.doBusiness(importCooOrgReqVo));
		} catch (Exception e) {
			logger.error("importV10PremisesHouseType error: {}", e);
			return null;
		}
	}

	/**
	 * 批量导入V10楼盘动态
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping(value = "/importV10PremisesDynamic", produces = "application/json")
	public String importV10PremisesDynamic(MultipartHttpServletRequest request, ImportCooOrgReqVo importCooOrgReqVo) {
		try {
			importCooOrgReqVo.setMulRequest(request);
			return JSONUtil.toJSONStringWithDateFormat(importPremisesDynamicHander.doBusiness(importCooOrgReqVo));
		} catch (Exception e) {
			logger.error("importV10PremisesDynamic error: {}", e);
			return null;
		}
	}

	/**
	 * 批量导入V10楼盘图片
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping(value = "/importV10PremisesImage", produces = "application/json")
	public String importV10PremisesImage(MultipartHttpServletRequest request, ImportCooOrgReqVo importCooOrgReqVo) {
		try {
			importCooOrgReqVo.setMulRequest(request);
			return JSONUtil.toJSONStringWithDateFormat(importPremisesImageHander.doBusiness(importCooOrgReqVo));
		} catch (Exception e) {
			logger.error("importV10PremisesImage error: {}", e);
			return null;
		}
	}

	/**
	 * 批量导入V10楼盘佣金
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping(value = "/importV10PremisesCommission", produces = "application/json")
	public String importV10PremisesCommission(MultipartHttpServletRequest request, ImportCooOrgReqVo importCooOrgReqVo) {
		try {
			importCooOrgReqVo.setMulRequest(request);
			return JSONUtil.toJSONStringWithDateFormat(importPremisesCommissionHander.doBusiness(importCooOrgReqVo));
		} catch (Exception e) {
			logger.error("importV10PremisesCommission error: {}", e);
			return null;
		}
	}

	/**
	 * 初始化数据
	 *
	 * @param request
	 * @param reqVo
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/initData", produces = "application/json")
	public String getPremises(HttpServletRequest request, @RequestBody InitDataReqVo reqVo)
			throws IOException {
		try {
			return JSONUtil.toJSONStringWithDateFormat(sendWXMsgAgainHander.hander(reqVo));
		} catch (Exception e) {
			logger.error("initData ==> getPremises ==> error: {}", e);
			return null;
		}
	}

	/**
	 * 初始化数据
	 *
	 * @param request
	 * @param reqVo
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/sendWXMsgAgain", produces = "application/json")
	public String sendWXMsgAgain(HttpServletRequest request, @RequestBody SendWXMsgAgainReqVo reqVo)
			throws IOException {
		try {
			return JSONUtil.toJSONStringWithDateFormat(sendWXMsgAgainHander.hander(reqVo));
		} catch (Exception e) {
			logger.error("initData ==> sendWXMsgAgain ==> error: {}", e);
			return null;
		}
	}

	@PostMapping("/selectWHSyncReportList")
	public SyncReportDto selectWHSyncReportList(@RequestBody List<Long> reportIds){
		SyncReportDto syncReportDto = whReportService.sendWHSyncReport(reportIds);
		logger.info("over report valid time count: {}", JSON.toJSONString(syncReportDto));
		return syncReportDto;
	}

	@PostMapping("/getWHSyncReport")
	public String getWHSyncReport(@RequestBody SyncReportDto syncReportDto){
		Map<String, String> returnMap = new HashMap<>();
		returnMap.put("Code", "0000");
		returnMap.put("Describe", "接受成功");
		return JSON.toJSONString(returnMap);
	}

	/**
	 * 重新发送推送失败记录.
	 *
	 * @param logIds 日志Ids
	 */
	@PostMapping("/sendWHSyncReportAgain")
	public void sendWHSyncReportAgain(@RequestBody List<Long> logIds){
		whReportService.sendWHSyncReportAgain(logIds);
	}

	/**
	 * 初始化公司权限、层级字段
	 */
	@PostMapping("/initDeptPermissionCode")
	public void initDeptPermissionCode(@RequestBody GenericIdRequest request){
		deptPermissionService.initDeptPermissionCode(request.getId());
	}
}
